package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_Erc_contractMapper;
import com.bc.bcapi.dao.T_EtherscanMapper;
import com.bc.bcmodel.domain.T_Erc_contractModel;
import com.bc.bcmodel.domain.T_EtherscanModel;
import com.bc.commoncore.util.HttpUtil;
import com.bc.commoncore.util.ParseHelper;
import com.bc.commonentity.common.JsonResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Name ReptilianService
 * @Description 爬虫类
 * @Author douhaichao
 * @Date 2018/9/11 17:18
 * @Version 1.0
 **/
@Service
public class ReptilianService {

    @Resource
    T_Erc_contractMapper t_erc_contractMapper;
    @Resource
    T_EtherscanMapper t_etherscanMapper;
    private String domain = "https://etherscan.io/";

    public JsonResult getEtherscan() {
        JsonResult js = new JsonResult();
        String homeUrl = "https://etherscan.io/tokens?p={index}";
        List(homeUrl);
        return js;
    }

    private void List(String url) {
        for (int i = 1; i <= 14; i++) {
            String listUrl = url.replace("{index}", i + "");
            String listHtml = HttpUtil.sendGet(listUrl, "");
            Document doc = Jsoup.parse(listHtml);
            Element element = doc.getElementById("ContentPlaceHolder1_divresult");
            Elements elements_a = element.getElementsByTag("h5");
            for (Element item :
                    elements_a) {
                Elements eleList = item.select("a[href]");
                for (Element link :
                        eleList) {
                    String href = link.attr("href");
                    String text = link.text();
//                    System.out.println(href);
//                    System.out.println(text);
                    Detail(href, text);
                }
//                System.out.println(url);
            }
        }
    }

    private void Detail(String url, String text) {
        url = domain + url;
//String token_name=text.substring()
        Pattern pattern = Pattern.compile("\\(.*?\\)");
        Matcher matcher = pattern.matcher(text);
        String symbol = matcher.find() ? matcher.group(0) : "";
        //简称
        String token_symbol = symbol.replace("(", "").replace(")", "");
        //全名称
        String token_name = text.replace("(" + token_symbol + ")", "");
        //契约地址
        String contact_id = "";
        //位
        String digit = "";
        String html = HttpUtil.sendGet(url, "");
        Document doc = Jsoup.parse(html);
        Element element = doc.getElementById("ContentPlaceHolder1_trContract");
        Elements elements = element.select("a[href]");
        for (Element item :
                elements) {
            contact_id = item.text();
        }
        T_EtherscanModel t_etherscanModelParam = new T_EtherscanModel();
        t_etherscanModelParam.setContact_id(contact_id);
        if (t_etherscanMapper.count(t_etherscanModelParam) > 0) {
            //如果存在当前契约ID就跳出
            return;
        }
        Element elementDecimal = doc.getElementById("ContentPlaceHolder1_trDecimals");
        digit = elementDecimal.text();
        digit = digit.replace("Decimals: ", "");
        T_EtherscanModel t_etherscanModel = new T_EtherscanModel();
        t_etherscanModel.setContact_id(contact_id);
        t_etherscanModel.setDigit(ParseHelper.ToInt(digit, 0));
        t_etherscanModel.setToken_name(token_name);
        t_etherscanModel.setToken_symbol(symbol);
        t_etherscanMapper.insert(t_etherscanModel);
    }
}
